/**
 * platform-core
 * @author Wade.Wang	Jun 16, 2006
 */
package org.jradweb.test.utils;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

public class SpringUtil {
	private static ApplicationContext ctx;

	public static ApplicationContext getApplicationContext()
			throws IOException, BeansException {
		if (ctx == null) {
			System.setProperty("WEB_CONFIG_HOME", "./src/main/config/test");
			String[] xmlfile = new String[] {
					"classpath:spring/*.xml" };
			ctx = new org.springframework.context.support.FileSystemXmlApplicationContext(
					xmlfile);
		}
		return ctx;
	}

	public static <T> T getComponent(Class<T> clazz) throws IOException,
			BeansException {
		return (T) getApplicationContext().getBean(clazz.getName());

	}

	public static <T> T getComponent(String name, Class<T> clazz)
			throws IOException, BeansException {
		return (T) getApplicationContext().getBean(name);

	}

	public static Object getComponent(String componentName) throws IOException,
			BeansException {
		return getApplicationContext().getBean(componentName.toString());
	}

}